package com.ib.review.controller;

import com.ib.review.base.response.JsonResult;
import com.ib.review.pojo.dto.AnalyzeRecordParam;
import com.ib.review.pojo.dto.AnalyzeRecordQuery;
import com.ib.review.pojo.dto.AnalyzeResultParam;
import com.ib.review.pojo.dto.AnalyzeResultQuery;
import com.ib.review.pojo.po.AnalyzeRecordPO;
import com.ib.review.pojo.vo.AnalyzeRecordVO;
import com.ib.review.pojo.vo.AnalyzeResultVO;
import com.ib.review.service.ParsingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
@RestController
@RequestMapping("/review/parsing")
public class ParsingController {

    private final ParsingService parsingService;
    @Autowired
    public ParsingController(ParsingService parsingService) {
        this.parsingService = parsingService;
    }

    @PostMapping("upload")
    public JsonResult<String> upload(@RequestParam("file") MultipartFile file,
                                     AnalyzeRecordParam param) {
        String filename = parsingService.upload(file, param);
        return JsonResult.ok(filename);
    }

    @GetMapping("queryRecord")
    public JsonResult<List<AnalyzeRecordPO>> queryRecord(AnalyzeRecordQuery analyzeRecordQuery) {
        List<AnalyzeRecordPO> list = parsingService.queryRecord(analyzeRecordQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("delete")
    public JsonResult<Void> deleteRecord(@RequestBody AnalyzeResultQuery analyzeResultQuery) {
        parsingService.deleteRecord(analyzeResultQuery);
        return JsonResult.ok();
    }

    @GetMapping("queryResult")
    public JsonResult<List<AnalyzeResultVO>> queryResult(AnalyzeResultQuery analyzeResultQuery) {
        List<AnalyzeResultVO> list = parsingService.queryResult(analyzeResultQuery);
        return JsonResult.ok(list);
    }

    @PostMapping("update")
    public JsonResult<Void> updateResult(@RequestBody AnalyzeResultParam analyzeResultParam) {
        parsingService.updateResult(analyzeResultParam);
        return JsonResult.ok();
    }

    @GetMapping("queryRecordById")
    public JsonResult<AnalyzeRecordVO> queryRecordById(AnalyzeRecordQuery analyzeRecordQuery) {
        AnalyzeRecordVO vo = parsingService.queryRecordById(analyzeRecordQuery);
        return JsonResult.ok(vo);
    }

    @GetMapping("exportToExcel")
    public ResponseEntity<byte[]> exportToExcel(@RequestParam("recordId") String recordId) {
        byte[] excelBytes = parsingService.exportToExcel(recordId);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        headers.setContentDispositionFormData("attachment", "data-export-" + timestamp + ".xlsx");

        return ResponseEntity.ok()
                .headers(headers)
                .body(excelBytes);
    }
}
